package loveqq.niceexcel.read.handler.internal;

import cn.hutool.core.util.StrUtil;
import loveqq.niceexcel.annotation.ExcelEnd;
import loveqq.niceexcel.annotation.internal.Internal;
import loveqq.niceexcel.core.metadata.EndHeadMeta;
import loveqq.niceexcel.read.*;
import loveqq.niceexcel.read.handler.CellReadHandler;
import loveqq.niceexcel.read.handler.LocateDataReadHandler;
import loveqq.niceexcel.read.handler.RowReadHandler;
import loveqq.niceexcel.read.handler.WorkbookReadHandler;
import loveqq.niceexcel.util.CellUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * @author zhongjh@tsintergy.com
 * @date 2022-08-18
 */
@Internal
public class ExcelEndReadHandler implements WorkbookReadHandler, RowReadHandler, CellReadHandler, LocateDataReadHandler {
    private final EndHeadMeta endHeadMeta = new EndHeadMeta();

    @Override
    public void onWorkbookBegin(ReadContext context, Workbook workbook) {
        ExcelEnd endAnno = context.getHeadClass().getAnnotation(ExcelEnd.class);
        if (endAnno != null) {
            endHeadMeta.setRowHead(endAnno.row());
            endHeadMeta.setColumnHead(endAnno.column());
            endHeadMeta.setIncludeSelf(endAnno.includeSelf());
        }
    }

    @Override
    public void onCellRead(ReadContext context, Cell cell) {
        if (!context.getMergedRegions().isRegionFirstCell(cell)) {
            return;
        }
        // 如果是表头名字且是合并区域的第一个单元格
        String headName = CellUtils.getCellData(context.getMergedRegions().getRegionFirstCell(cell)).getStringValue();
        if (StrUtil.isNotEmpty(headName) && headName.equals(endHeadMeta.getRowHead())) {
            endHeadMeta.setRowIndex(cell.getRowIndex());
            endHeadMeta.setRowRegion(context.getMergedRegions().getRegion(cell));
        }
        if (StrUtil.isNotEmpty(headName) && headName.equals(endHeadMeta.getColumnHead())) {
            endHeadMeta.setColumnIndex(cell.getColumnIndex());
            endHeadMeta.setColumnRegion(context.getMergedRegions().getRegion(cell));
        }
    }

    @Override
    public boolean beforeLocateData(ReadContext context, Cell cell) {
        // 列结束且要求读取此列
        if (endHeadMeta.getColumnRegion() != null && endHeadMeta.isIncludeSelf()
                && cell.getColumnIndex() > endHeadMeta.getColumnRegion().getLastColumn()) {
            return false;
        }
        // 列结束且要求不读取此列
        if (endHeadMeta.getColumnRegion() != null && !endHeadMeta.isIncludeSelf()
                && cell.getColumnIndex() >= endHeadMeta.getColumnRegion().getFirstColumn()) {
            return false;
        }
        // 行结束且要求不读取此行(超过该行区域重新解析)
        if (endHeadMeta.getRowRegion() != null && !endHeadMeta.isIncludeSelf()
                && cell.getRowIndex() <= endHeadMeta.getRowRegion().getLastRow()) {
            context.getLocateDataHeadMetas().clearAndResetAllIndex();
            return false;
        }
        return true;
    }

    @Override
    public void onRowEnd(ReadContext context, Row row) {
        // 行结束且要求已读取此行(超过该行区域重新解析)
        if (endHeadMeta.getRowRegion() != null && endHeadMeta.isIncludeSelf()
                && row.getRowNum() == endHeadMeta.getRowRegion().getLastRow()) {
            context.getLocateDataHeadMetas().clearAndResetAllIndex();
        }
    }
}
